#include <bits/stdc++.h>
using namespace std;
const int N = 55, M = 1010, K = 25;
const int INF = 0x3f3f3f3f;
int n;
char s[M];
int tr[N * K][4], cnt[N * K], idx;
int fail[N * K], q[N * K];
int f[M][N * K];
int get(char c) {
  switch (c) {
    case 'A':
      return 0;
    case 'G':
      return 1;
    case 'C':
      return 2;
    case 'T':
      return 3;
    default:
      return -1;
  }
}

void build() {
  int tt = -1, hh = 0;
  for (int i = 0; i < 4; ++i)
    if (tr[0][i]) q[++tt] = tr[0][i];
  while (hh <= tt) {
    int u = q[hh++];
    for (int i = 0; i < 4; ++i) {
      if (tr[u][i]) {
        fail[tr[u][i]] = tr[fail[u]][i];
        q[++tt] = tr[u][i];
      } else {
        tr[u][i] = tr[fail[u]][i];
      }
    }
  }
}

void insert(char s[]) {
  int p = 0;
  for (int i = 0; s[i]; ++i) {
    int u = get(s[i]);
    if (!tr[p][u]) tr[p][u] = ++idx;
    p = tr[p][u];
  }
  cnt[p] = 1;
}

void solve() {
  // Initialize
  memset(fail, 0, sizeof fail);
  memset(cnt, 0, sizeof cnt);
  memset(f, 0x3f, sizeof f);
  memset(tr, 0, sizeof tr);
  f[0][0] = 0;
  idx = 0;
  // Input and Build the AC Automaton
  for (int i = 1; i <= n; ++i) cin >> s, insert(s);
  cin >> s + 1;
  build();
  int n = strlen(s + 1);
  // Dymanic Programming
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= idx; ++j) {
      for (int k = 0; k < 4; ++k) {
        int cost = get(s[i + 1]) != k;  // 修复下一个的费用
        int p = tr[j][k];
        bool flag = true;
        int temp = p;
        while (temp) {
          if (cnt[temp]) {
            flag = false;
            break;
          } else {
            temp = fail[temp];
          }
        }
        if (flag) f[i + 1][p] = min(f[i + 1][p], f[i][j] + cost);
      }
    }
  }
  int res = INF;
  for (int j = 0; j <= idx; ++j) res = min(res, f[n][j]);
  if (res == INF) res = -1;
  cout << res << endl;
}

int main() {
  int T = 1;
  while (cin >> n, n) {
    cout << "Case " << T++ << ": ";
    solve();
  }
}
